<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
    <title>新增借阅</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    
    <script src="../../scripts/boot.js" type="text/javascript"></script>
    

    <style type="text/css">
    html, body
    {        
        padding:0;
        margin:0;
        border:0;
        height:100%;
        overflow:hidden;
    }
    </style>
</head>
<body>    
     
    <form id="form1" method="post">
        <fieldset style="border:solid 1px #aaa;padding:3px;">
            <legend >新增借阅信息</legend>
            <div style="padding:5px;">
        <table>
            <tr>
                <td style="width:80px;">所借图书:</td>
                <td style="width:150px;">    
                    <input id="bookid" name="bookid" emptyText="请选择..."class="mini-combobox" url="/borrowinfoaddaction?method=queryBookinfo"
                    required="true" onValueChanged = "onBookIdChanged"/>
                </td>
                <td style="width:80px;">借阅编号：</td>
                <td >                        
                    <input id="borrowid" name="borrowid" class="mini-textbox" required="true" readonly="true"/>
                        
                </td>
                
            </tr>
            <tr>
                <td >借阅人：</td>
                <td >    
                    <input id="borrower" name="borrower" class="mini-textbox" required="true"  emptyText="请输入借阅人" requiredErrorText="不能为空"/>
                </td>
                <td >联系方式：</td>
                <td >    
                     <input id="phone" name="phone" class="mini-textbox" required="true" emptyText="请输入联系方式" requiredErrorText="不能为空" onvalidation="onPhoneValidation"/>
                </td>
            </tr>
            <tr>
                <td >借阅时间：</td>
                <td >    
                    <input id="borrowtime"name="borrowtime"class="mini-datepicker" allowinput="flase" 
                    format="yyyy-MM-dd HH:mm:ss" showTime="true" timeFormat="HH:mm:ss" showOkButton="true" showClearButton="false" required="true" 
                    ondrawdate="onDrawDate"/>
                </td>
                <td >剩余数量：</td>
                <td >    
                     <input id="remain" name="remain" class="mini-textbox"  readonly="true" />
                </td>
            </tr>          
        </table>            
            </div>
        </fieldset>
        <div style="text-align:center;padding:10px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">保存</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div>        
    </form>
    <script type="text/javascript">
        mini.parse();
        //打开页面加载初始值
        setFormValue();


        var form = new mini.Form("form1");
        //设置初始值
        function  setFormValue(){
        	$.ajax({
        		url:"/borrowinfoaddaction?method=getNextID",
        		type:'post',
        		cache:false,
        		success:function(text){
        			mini.get("borrowid").setValue(text);
        			mini.get("borrowtime").setValue(new Date());
        		},
        		error:function (jqXHR,textStatus,errorThrow){
        			mini.alert(jqXHR.responseText);
        			CloseWindow();
        		}
        	})
        }
        //时间范围设置
		function onDrawDate(e){
			var date=e.date;
			var d=new Date();
			if(date.getTime()>d.getTime()){
				e.allowSelect=false;
			}
		}
        //电话字段验证
        function onPhoneValidation(e){
             var value = mini.get("phone").getValue();

             const phone_regx = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
             //var regTel2 = /^(\d{7,8})(-(\d{3,}))?$/.test(value);//不带区号的固定电话
             if (value != "") {
               if (!phone_regx.test(value)) { 
                 mini.alert("电话号码输入有误！");
                 obj.focus();
                 return false;
               }
             }
             else {
            	 mini.alert("请输入电话号码！");
               return false;
             }

             return true;
        }
        function SaveData() {
            var o = form.getData();            

            form.validate();
            if (form.isValid() == false) {
            	return;
            }
            var json = mini.encode(o);
            $.ajax({
                url: "/borrowinfoaddaction?method=addBorrowInfo",
		        type: 'post',
                data: { data: json },
                cache: false,
                success: function (text) {
                    mini.alert(text,"提示：",function(){
                    	 CloseWindow("save");
                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }

         function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {            
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();            
        } 
        function onOk(e) {
            SaveData();
        }
         function onCancel(e) {
            CloseWindow("cancel");
        }
//级联更新剩余数量
function onBookIdChanged(e){
	var bookid=e.value;
	$.ajax({
		url:"/borrowinfoaddaction?method=queryBookinfoRemain&bookid="+bookid,
		type:'post',
		cache:false,
		success:function(text){
			mini.get("remain").setValue(text);
		},
		error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);
            CloseWindow();
        }
	})
}
//克隆子页面
function SetData(data) {
        //跨页面传递的数据对象，克隆后才可以安全使用
        if(data.action=="AddBorrInBook"){
        	var data = mini.clone(data);
        	var bookid=data.bookid;
			var remain=data.remain;
            $.ajax({
                url: "/borrowinfoaddaction?method=getBookNameById&bookid="+bookid,
                type:'post',
                cache:false,
                /* data:{data:data.bookid}, */
                success: function (text) {
                    mini.get("bookid").setValue(bookid);
                    mini.get("bookid").setText(text);
                    mini.get("remain").setValue(remain);
                    mini.get("bookid").setEnabled(false);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        	
        }
} 

    </script>
</body>
</html>